<?
if (!defined('MODULE_FILE')){
die ("cant access file directly!");
}

$mid 	= ( ( true == isset( $_GET['mid'] ) && true == valStr( $_GET['mid'] ) ) ? $_GET['mid'] : NULL );
$smid 	= ( ( true == isset( $_GET['smid'] ) && true == valStr( $_GET['smid'] ) ) ? $_GET['smid'] : NULL );

autoLoader();

$objBasePatient 	= new BasePatients();
$objPatients 		= new Patients();
$objPatientTypes 	= new PatientCatagories();

?>
<html>
<head>
<?php require_once 'commonJS.php'; ?>
<script>
  $(document).ready(function(){
          $( "#from, #birth_date" ).datepicker({
            dateFormat: 'dd-mm-yy',
            showOn: "button",
			buttonImage: "images/calendar.gif",
			buttonImageOnly: true,
            changeMonth: true,
            changeYear: true,
            yearRange: "-90"
          });
       });

	jQuery(document).ready(function(){
		$( "#patient_name" ).combogrid({
			url: 'modules.php?file=fetch_server&type=fetch&action=searchPatient',
			debug:true,
			//replaceNull: true,
			colModel: [{'columnName':'id','width':'10','label':'Id'}, {'columnName':'name','width':'60','label':'Full Name'},{'columnName':'Occupation','width':'30','label':'Occupation'}],
			select: function( event, ui ) {
				$( "#patient_name" ).val( ui.item.name );
				$( "#patient_id" ).val( ui.item.id );
				return false;
				}
		});
	});
</script>

	<link rel="stylesheet" type="text/css" media="screen" href="js/Autocomplete/resources/css/smoothness/jquery-ui-1.10.1.custom.css"/>
	<link rel="stylesheet" type="text/css" media="screen" href="js/Autocomplete/resources/css/smoothness/jquery.ui.combogrid.css"/>
	
	<script type="text/javascript" src="js/Autocomplete/resources/jquery/jquery-ui-1.10.1.custom.min.js"></script>
	<script type="text/javascript" src="js/Autocomplete/resources/plugin/jquery.ui.combogrid-1.6.3.js"></script>
	
</head>
<body>
<br>
<div class="col-lg-12">
<div id="main_contianer">
	<div>
		<div style="width: 200px;">
			<a href="modules.php?file=case_papers&mid=<? echo $mid; ?>&smid=<? echo $smid; ?>" title="New Case Paper"><img src="images/edit_add.gif" width="20" height="20">New case paper</a>
		</div>
	</div>
	<br>
	<table>
		<tr>
			<td>
				<div style="height: 262px;margin-left:10px;width:390px;" class="panel panel-primary">
					<div class="panel-heading">
						Search by Patient ID OR Patient Name
					</div>
					<div style="margin:20px 0px 0px 20px;">
						<input type="text" name="patient_id" id="patient_id" value="" placeholder="Patient ID.." /> OR 
						<input type="text" name="patient_name" id="patient_name" value="" placeholder="Patient Name.." />
						<div id="switcher" style="float:right"></div>
						<label id="search_data"></label> 
					</div>
					<div style="display: none;margin:30px 5px 0px 20px;" class="alert alert-success" id="success_display">
						<img alt="Patient Found with info.." src="images/ok.png" width="20" height="20">
						<label id='disp_patient_name'></label>
					</div>
					<div style="display: none;margin:30px 5px 0px 20px;" class="alert alert-danger" id="failed_display">
						<img alt="Patient Found with info.." src="images/close.png" width="20" height="20">
						<label id='disp_failed'></label>
					</div>
					<div style="margin:50px 0px 0px 20px;">
					<input type="button" name="search_patient" value="Search..." onclick="searchByIdPatient()" /> &nbsp; &nbsp;
					<input type="hidden" name="mid" id="mid" value="<? echo $mid;?>" />
					<input type="hidden" name="smid" id="smid" value="<? echo $smid;?>" />
					
					<a href="modules.php?file=case_papers&mid=<? echo $mid;?>&smid=<? echo $smid;?>" style="color: black;">
						<input type="button" name="add_new_patient" value="Add New Patient.."/>
					</a>
					</div>
				</div>
			</td>
			<td>
				<div style="height: 290px;margin-left:10px;width:550px;display: none;" class="panel panel-primary" id="old_case_paper_amount">
				<div class="panel-heading">
					Found Patient Details
				</div>
					<div style="padding: 10px;">
					<form action="modules.php?file=insert_case_paper&type=insert" name="quick_from" id="quick_from" method="post">
						<div id="patient_info"></div>
						<table width="100%">
							<tr>
								<td>
									Amount:
								</td>
								<td>
									<input type="text" name="case_paper_charge" id ="case_paper_charge" value="" />
									<input type="hidden" name="f_patient_id" id="f_patient_id" value="" />
									<input type="hidden" name="f_case_paper_id" id="f_case_paper_id" value="" />
								</td>
							</tr>
						</table>
						<div style="clear: both;" id="meesgae">&nbsp;</div>
						<input type="button" value="SAVE" name="save" id='save' onclick="saveQuickFrom()" />
						<input type="button" value="Print" name="print" id="print" style="display: none;" />
						<a href="modules.php?file=case_papers&mid=<? echo $mid;?>&smid=<? echo $smid;?>" style="color: black;" title="Go to Reception Module.."><input type="button" value="Cancel" name="cancel" /></a>
					</form>
					</div>
				</div>
			</td>
		</tr>
		</table>
	</div>
</div>
</body>
</html>

<script>
	function saveQuickFrom() {
		var intPatientId 	= $( '#f_patient_id' ).val();
		var intCasePaperId 	= $( '#f_case_paper_id' ).val();
		var intAmount 		= $( '#case_paper_charge' ).val();
		var strURL = "&mid=" + $('#mid').val() + "&smid=" + $('#smid').val();
		
		$.ajax({
			url: "modules.php?file=insert_case_paper&action=quick_insert&type=insert&charge_amount=" + intAmount + "&patient_id=" + intPatientId + "&case_paper_id=" + intCasePaperId,
			type: "post",
			data: "amount=" + intAmount + strURL,
			success: function( returnData ) {
				strCustomerData = returnData;

				$( '#print' ).show();
				$( '#save' ).hide();

				$( '#meesgae' ).html( "Case Paper renewed successfully." );
				console.log( strCustomerData ); 
				$( '#main_contianer' ).html( strCustomerData );
			},
			error:function(){
				alert( "! Delete Failed please check Item have Id set for delete." );
			}
		});
	}
	function searchByIdPatient() {
		var intPatientId =	$('#patient_id').val();

		if( '' == intPatientId || null == intPatientId ) {
			if( '' == $('#patient_name').val() || null == $('#patient_name').val() ) {
				$( '#failed_display' ).css( "display", "block" );
				$( '#disp_failed' ).html( "Please enter id or name of patient." );
			}
			
			var strPatientName = $('#patient_name').val();
			$strUrl = "modules.php?file=search_patient&type=search&patient_name=" + strPatientName;
		} else {
			$strUrl = "modules.php?file=search_patient&type=search&patient_id=" + intPatientId;
		}

		$.ajax({
			//url: "search/search_patient.php",
			url: $strUrl,
			type: "post",
			data: intPatientId,
			success: function( returnData ) {
				strCustomerData = returnData;
		
				if( 'not found::' == strCustomerData ) {
					$( '#failed_display' ).css( "display", "block" );
					$( '#disp_failed' ).html( "Patient not found with id :: " + $('#patient_id').val() );
					$( '#success_display' ).css( "display", "none" );
					$( '#old_case_paper_amount' ).css( "display", "none" );
				} else {
					$( '#failed_display' ).css( "display", "none" );
					$( '#success_display' ).css( "display", "block" );
					$( '#old_case_paper_amount' ).css( "display", "block" );
					
					var strData = strCustomerData.split( '_' );
					
					$( '#disp_patient_name' ).html( "Patient found with Name :: " + strData[0] + " " + strData[1] + " " + strData[2] );
					$( '#f_disp_patient_id' ).html( strData[0] );
					$( '#f_patient_id' ).val( strData[0] );
					$( '#f_case_paper_id' ).val( strData[3] );
					$( '#f_patient_name' ).html( strData[1] + " " + strData[2] );
					$( '#save' ).show();
					$( '#print' ).hide();

					displayPatientInfo( strData[0] );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function displayPatientInfo( intPatientId ) {

		$strUrl = 'modules.php?file=patient_info&type=search&patient_id=' + intPatientId;
		
		$.ajax({
			url: $strUrl,
			type: "post",
			data: "patient_data=" + intPatientId,
			success: function( returnData ) {
				strPatientData = returnData;
		
				if( 'not found::' == strPatientData ) {
					$( '#patient_info' ).html( '' );
					$( '#patient_info' ).css( "display", "none" );
				} else {
					$( '#patient_info' ).html( strPatientData );
					$( '#patient_info' ).css( "display", "block" );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}
</script>